#ifndef MULTIDATASAMEND_H
#define MULTIDATASAMEND_H
#include <QMutex>
#include <ctime>
#include <QtWidgets>
#include "solar/beacon/Field.h"
#include "tools/Gadget.h"
#include <QSqlQuery>
#include <QSqlError>


class MultidatasAmend : public QObject, public QRunnable{

    Q_OBJECT

public:

    MultidatasAmend(QMap<QString, QMap<QString, QString>> &_RGLRLMAP_,
                    QMap<ThreadPoint, NT::THREAD_STATE> &_pool_thread_,
                    QMap<QString, bool> &_pool_bool_,
                    QMap<QString, QString> &_pool_chars_,
                    QMap<QString, QList<QString>> &_pool_ststr_,
                    QMap<QString, NT::CUT_MEDIA_TYPE> &_pool_mtpltype_,
                    QSqlDatabase *db);

    void run();

private:
    QMap<QString, bool> *pool_bool;
    QMap<QString, int> *pool_int;
    QMap<QString, QString> *pool_chars;
    QMap<QString, QList<QString>> *pool_ststr;
    QMap<QString, QList<int>> *pool_stint;
    QMap<QString, NT::CUT_MEDIA_TYPE> *pool_mtpltype;
    QMap<ThreadPoint, NT::THREAD_STATE> *pool_thread;
    QSqlQuery Query;
    QSqlDatabase *DB;
    QString HyperLink;
    Extend definfc;
    KEY_ATTRIBUTE RPLKEY;
    QMap<QString, bool> RdoMap_Bool;

signals:
    void amend_otherness_update(QList<int>);
    void amend_media_max(int);
    void amend_cover_find();
    void finish();
};

class MultidatasTake : public QObject, public QRunnable{

    Q_OBJECT

public:
    MultidatasTake(QMap<ThreadPoint, NT::THREAD_STATE> &_pool_thread_,
                   QMap<QString, QList<qint64>> &_pool_stint_,
                   QMap<QString, QList<QString>> &_pool_ststr_,
                   QMap<QString, qint64> &_pool_int_,
                   QSqlDatabase *db)
   {
        pool_thread = &_pool_thread_;
        pool_int = &_pool_int_;
        pool_stint = &_pool_stint_;
        pool_ststr = &_pool_ststr_;
        Query = QSqlQuery(*db);
    }

    void run(){
        QMutex lock;
        lock.lock();
        QList<qint64> indexs = (*pool_stint)["maint-mark-selects"];
        QStringList reserve_duty_path = (*pool_ststr)["reserve-duty-path"];
        int lenght = indexs.size();
        if (!reserve_duty_path.isEmpty())
        {
            int index = 0;
            while (index < lenght)
            {
                if ((*pool_thread)[ThreadPoint::MULTI_REMOVE] == NT::THREAD_TERMINATED)
                {
                    break;
                }
                else if ((*pool_thread)[ThreadPoint::MULTI_REMOVE] == NT::THREAD_PAUSE)
                {
                    while (true)
                    {
                        if ((*pool_thread)[ThreadPoint::MULTI_REMOVE] == NT::THREAD_PAUSE)
                        {
                            QThread::msleep(15);
                            continue;
                        }
                        else
                        {
                            break;
                        }
                    }
                }
                else
                {
                    QString path = reserve_duty_path[indexs[index]];
                    QFile f(path);
                    switch ((*pool_int)["mtrs-remove-way"])
                    {
                        case 0:
                            f.remove();
                            Query.exec(QString("DELETE FROM media_datum WHERE mdrs_path='%1';").arg(path));
                            break;

                        case 1:
                            Query.exec(QString("DELETE FROM media_datum WHERE mdrs_path='%1';").arg(path));qDebug() << "删除线程执行种";
                            break;

                    }
                    QThread::msleep(10);
                }
                index++;
            }
        }
        lock.unlock();
        emit rmve_finish();
        (*pool_thread)[ThreadPoint::MULTI_REMOVE] = NT::THREAD_IDLEABOUT;
    }

private:
    QMap<QString, qint64> *pool_int;
    QMap<QString, QList<qint64>> *pool_stint;
    QMap<QString, QList<QString>> *pool_ststr;
    QMap<ThreadPoint, NT::THREAD_STATE> *pool_thread;
    QSqlQuery Query;
    int index = 0;

signals:
    void rmve_finish();
};

#endif // MULTIDATASAMEND_H
